package vip.specimen.system.controller.authority;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import vip.specimen.system.common.annotation.SystemJurisdiction;
import vip.specimen.system.common.enums.ResultCode;
import vip.specimen.system.datasource.entity.SystemUserEntity;
import vip.specimen.system.datasource.validation.SystemUserInsertValidation;
import vip.specimen.system.datasource.validation.SystemUserValidation;
import vip.specimen.system.result.JsonResult;
import vip.specimen.system.service.SystemUserService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/15 11:53
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Api(tags = "管理员")
@RestController
@RequestMapping(value = "/authority/manage")
public class ManageController {
    @Resource
    private SystemUserService userService;

    @GetMapping
    @RequiresPermissions(value = "authority:manage:select")
    @ApiOperation(value = "查看", notes = "获取系统管理员列表")
    @SystemJurisdiction(resourceType = "button", permissionName = "查看管理员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult<Page<SystemUserEntity>> manageAll() {
        return JsonResult.success(this.userService.findAll());
    }

    @PutMapping
    @RequiresPermissions(value = "authority:manage:insert")
    @ApiOperation(value = "新增", notes = "根据实体添加系统管理员")
    @SystemJurisdiction(resourceType = "button", permissionName = "添加用户")
    public JsonResult<SystemUserEntity> insert(@RequestBody @Valid SystemUserInsertValidation userValidation) {
        return JsonResult.success(this.userService.insert(userValidation));
    }

    @PostMapping()
    @RequiresPermissions(value = "authority:manage:update")
    @ApiOperation(value = "更新", notes = "根据uuid更新用户信息")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新用户信息")
    public JsonResult update(@RequestParam("uuid") UUID uuid, @RequestBody @Valid SystemUserValidation userValidation) {
        SystemUserEntity result = this.userService.update(uuid, userValidation);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @DeleteMapping
    @RequiresPermissions(value = "authority:manage:delete")
    @ApiOperation(value = "删除", notes = "根据uuid删除用户")
    @SystemJurisdiction(resourceType = "button", permissionName = "删除用户")
    public JsonResult delete(@RequestBody HashMap<String, UUID> param) {
        return this.userService.delete(param.get("uuid")) ? JsonResult.success() : JsonResult.failed(ResultCode.ERROR_DELETE);
    }

    @PostMapping(value = "enable")
    @RequiresPermissions(value = "authority:manage:update:enable")
    @ApiOperation(value = "更新状态", notes = "根据uuid更新用户状态")
    @SystemJurisdiction(resourceType = "button", permissionName = "更新用户状态")
    public JsonResult<SystemUserEntity> update(@RequestParam("uuid") UUID uuid) {
        SystemUserEntity result = this.userService.update(uuid);
        return null == result ? JsonResult.failed(ResultCode.ERROR_SQL_UPDATE_SERVER) : JsonResult.success(result);
    }

    @GetMapping(value = "search")
    @ApiOperation(value = "搜索", notes = "根据关键词搜索用户列表")
    @RequiresPermissions(value = "authority:manage:search")
    @SystemJurisdiction(resourceType = "button", permissionName = "搜索用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录条数", dataType = "int")
    })
    public JsonResult findByNameAll(@RequestParam(value = "name", required = false, defaultValue = "") String name) {
        return JsonResult.success(this.userService.findByUsernameContainingOrderByCreateTimeDesc(name));
    }
}
